Skip to content

fix(logger): child Logger without a level follows its parent level again - #8842

Open
AtulJoshi1206 wants to merge 1 commit into
aws-powertools:developfrom
AtulJoshi1206:fix/logger-child-level-8778
Open

AtulJoshi1206 wants to merge 1 commit into
aws-powertools:developfrom
AtulJoshi1206:fix/logger-child-level-8778

Conversation

@AtulJoshi1206

Copy link
Copy Markdown
Contributor

Issue number: closes #8778

Summary

Changes

Since #5950 a child Logger always calls setLevel(). Without a level it resolves to INFO (or POWERTOOLS_LOG_LEVEL) and stops following its parent. This PR keeps #5950's behaviour for children that pass a level and restores inheritance for children that don't:

  • _init_logger: a child only sets its level when one is passed. Otherwise it stays NOTSET, like before 3.6.0.
  • initial_log_level: NOTSET for a child without a level. Before this, refresh_sample_rate_calculation() (which inject_lambda_context calls on warm invocations) pinned the child back to INFO.
  • log_level: returns the effective level, so a child reports the level it actually logs at. That also makes copy_config_to_registered_loggers(source_logger=child) copy that level. Without this, the first change alone would make child.log_level drop from 20 to 0. This is the open question from the issue thread. I went with the effective level because returning 0 would change what log_level reports for every default child today, but I'm happy to flip it if you'd rather match 3.5.0 exactly.
  • One sentence in the Child Loggers docs, plus the level docstring.

The priority doesn't change: ALC, then an explicit level (constructor or setLevel), then POWERTOOLS_LOG_LEVEL. A child with an explicit level still goes through the same resolution, including the ALC warning.

User experience

Before (3.6.0 to 3.41.0): with Logger(level="DEBUG") in app.py and Logger(child=True) in shared.py, the child's DEBUG logs were dropped. The same happened with setLevel("DEBUG") or sampling_rate=1 on the parent. With a WARNING parent, the child still emitted INFO.

After: the child follows its parent again, as in 3.5.0, and a child with its own level still uses it.

Visible differences from the current release:

  • With POWERTOOLS_LOG_LEVEL=DEBUG and an explicit Logger(level="INFO") parent, the child now follows the parent (INFO), as it did in 3.5.0.
  • child.log_level returns the inherited level. It's 20 with a default parent, so that case is unchanged.
  • The proxied stdlib child.level is NOTSET again.

Verification

  • New tests: I added 9 functional tests, using the public API only. 7 fail on current develop and pass with the fix. The 2 ALC tests pass on both and guard that ALC still wins for children. No existing test was changed. They cover the cases @saimcyber listed: a parent setLevel() after the child is created, sampling on a warm invocation, and a child with its own level.

  • Logger suites: on current develop (Python 3.14), pytest tests/unit/logger tests/functional/logger gives 213 passed.

  • Lint and types: ruff format --check, ruff check and mypy are clean.

  • Full suite: on Python 3.12, pytest tests/unit tests/functional -m "not perf" passed apart from test_datadog_write_to_log_with_env_variable. That test also fails in a full run on develop and passes on its own, so it's order-dependent and unrelated.

  • Cross-version check: I ran the same two-module app against 3.5.0, 3.6.0, 3.38.0 and the fix, covering:

    • a constructor level, setLevel, and sampling with the decorator;
    • a WARNING parent;
    • POWERTOOLS_LOG_LEVEL;
    • AWS_LAMBDA_LOG_LEVEL set to WARN, INFO and DEBUG.

    The branch matches 3.5.0 everywhere, except that fix(logger): child logger must respect log level #5950's explicit child level still works. There's no AWS service integration here, so I didn't deploy it; the ALC paths are covered by setting AWS_LAMBDA_LOG_LEVEL.

AI assistance: an AI coding assistant helped prepare this change; it was reviewed and the tests were run locally.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Since aws-powertools#5950 a child Logger always called setLevel(), so without an explicit
level it resolved to INFO (or the env var) and stopped inheriting the parent's
level. DEBUG logs from child modules were lost when the parent used DEBUG,
setLevel() or sampling, and INFO logs leaked when the parent used WARNING.

A child now only sets a level when one is passed, and otherwise stays NOTSET
like before 3.6.0. The same applies to refresh_sample_rate_calculation(), which
used to pin a child to INFO on warm invocations. log_level returns the
effective level, so a child reports, and copy_config_to_registered_loggers
copies, the level the child actually logs at.

Explicit child levels (aws-powertools#5950) and the ALC > explicit level > env var priority
are unchanged.
@AtulJoshi1206
AtulJoshi1206 requested a review from a team as a code owner November 16, 2026 14:52
@boring-cyborg boring-cyborg Bot added documentation Improvements or additions to documentation logger tests labels Nov 16, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 16, 2026
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation logger size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: child Logger without a level ignores its parent's log level since 3.6.0

1 participant